home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1996 #14 / Monster Media No. 14 (April 1996) (Monster Media, Inc.).ISO / prog_d / oleauttr.zip / XLOLE7.ZIP / XLCLS.PAS < prev    next >
Pascal/Delphi Source File  |  1996-01-04  |  4KB  |  166 lines

  1. unit Xlcls;
  2. {
  3. // XLCLS.PAS (C) 1995 W. Raike
  4. //              ALL RIGHTS RESERVED.
  5. }
  6. interface
  7. {*******************************************************************}
  8. {
  9.   Classes to "mirror" (some of) the properties and methods
  10.   exposed by Microsoft Excel as an OLE Automation server.
  11.  
  12.   Mirror classes are derived from SafeOLE unit's TSafeOleObject
  13.   instead of the basic TOLEObject so that nested objects
  14.   created "on the fly" by the ConnectInterface constructor can
  15.   be freed later.
  16.  
  17. }    
  18. {*******************************************************************}
  19.  
  20. uses
  21.   SysUtils, OleAuto, SafeOle;
  22.  
  23. type
  24.  
  25.   TExcelCells = class (TSafeOleObject)
  26.   private
  27.     procedure SetValue (ValText : String);
  28.   public
  29.     property Value : String write SetValue;
  30.   end;
  31.  
  32.   TExcelSheet = class (TSafeOleObject)
  33.   private
  34.     fCell : TExcelCells;
  35.     function GetCell (i,j: Integer): TExcelCells;
  36.   public
  37.     procedure FileSave (FileSaveName : string);
  38.     property Cells[X,Y: Integer] : TExcelCells read GetCell;
  39.   end;
  40.  
  41.   TXLWorkBook = class(TSafeOleObject)
  42.   private
  43.     function GetActiveSheet : TExcelSheet;
  44.     function GetSaved : Boolean;
  45.     procedure SetSaved(IsSaved : Boolean);
  46.   public
  47.     property ActiveSheet : TExcelSheet read GetActiveSheet;
  48.     property Saved : Boolean read GetSaved write SetSaved;
  49.   end;
  50.  
  51.   TXLWorkBooks = class(TSafeOleObject)
  52.   public
  53.     function Add : TXLWorkBook;
  54.   end;
  55.  
  56.   TExcelApp = class (TSafeOleObject)
  57.   private
  58.     function GetVisible : Boolean;
  59.     procedure SetVisible(vis : Boolean);
  60.     function GetWorkBooks : TXLWorkBooks;
  61.   public
  62.     property Visible : Boolean read GetVisible write SetVisible;
  63.     property WorkBooks : TXLWorkBooks read GetWorkBooks;
  64.     procedure Quit;
  65.   end;
  66.  
  67. implementation
  68.  
  69. procedure TExcelCells.SetValue(ValText : String);
  70. var
  71.   pszText : PChar;
  72. begin
  73.   pszText := StrAlloc(256);
  74.   StrPCopy(pszText, ValText);
  75.   SetOleProperty ('Value', 'PChar', pszText);
  76.   StrDispose(pszText);
  77. end;
  78.  
  79. function TExcelSheet.GetCell (i,j: Integer): TExcelCells;
  80. var
  81.   piCell : PInterface;
  82. begin
  83.   SetOLEMethodArg ('Integer',i);
  84.   SetOLEMethodArg ('Integer',j);
  85.   CallOLEFunction ('Cells', 'pInterface', piCell);
  86.   fCell := TExcelCells.ConnectInterface (piCell);
  87.   Result := fCell;
  88. end;
  89.  
  90. procedure TExcelSheet.FileSave (FileSaveName : string);
  91. var
  92.   arName : array[0..256] of Char;
  93.   pName  : PChar;
  94. begin
  95.   StrPCopy (arName, FileSaveName);
  96.   pName := @arName;
  97.   SetOleMethodArg ('PChar',pName);
  98.   CallOleProc ('SaveAs');
  99. end;
  100.  
  101. function TXLWorkBook.GetActiveSheet : TExcelSheet;
  102. var
  103.   piActiveSheet : pInterface;
  104. begin
  105.   GetOleProperty('ActiveSheet', 'pInterface', piActiveSheet);
  106.   if Assigned(piActiveSheet) then
  107.     Result := TExcelSheet.ConnectInterface(piActiveSheet);
  108. end;
  109.  
  110. function TXLWorkBook.GetSaved : Boolean;
  111. var
  112.   intSaved : Integer;
  113. begin
  114.   GetOleProperty('Saved', 'Integer', intSaved);
  115.   Result := Boolean(intSaved);
  116. end;
  117.  
  118. procedure TXLWorkBook.SetSaved(IsSaved : Boolean);
  119. var
  120.   intSaved : Integer;
  121. begin
  122.   intSaved := Integer(IsSaved);
  123.   SetOleProperty('Saved', 'Integer', intSaved);
  124. end;
  125.  
  126. function TXLWorkBooks.Add : TXLWorkBook;
  127. var
  128.   piNewBook : pInterface;
  129. begin
  130.   CallOleFunction('Add', 'pInterface', piNewBook);
  131.   if Assigned(piNewBook) then
  132.     Result := TXLWorkBook.ConnectInterface(piNewBook);
  133. end;
  134.  
  135. function TExcelApp.GetVisible : Boolean;
  136. var
  137.   intvis : Integer;
  138. begin
  139.   GetOleProperty('Visible', 'Integer', intvis);
  140.   Result := Boolean(intvis);
  141. end;
  142.  
  143. function TExcelApp.GetWorkBooks : TXLWorkBooks;
  144. var
  145.   piWorkBooks : pInterface;
  146. begin
  147.   GetOleProperty('WorkBooks', 'pInterface', piWorkBooks);
  148.   if Assigned(piWorkBooks) then
  149.     Result := TXLWorkBooks.ConnectInterface(piWorkBooks);
  150. end;
  151.  
  152. procedure TExcelApp.SetVisible(vis : Boolean);
  153. var
  154.   intvis : Integer;
  155. begin
  156.   intvis := Integer(vis);
  157.   SetOleProperty('Visible', 'Integer', intvis);
  158. end;
  159.  
  160. procedure TExcelApp.Quit;
  161. begin
  162.   CallOleProc('Quit');
  163. end;
  164.  
  165. end.
  166.